package mn.slide.petrovis;

import java.util.ArrayList;
import java.util.List;
import mn.slide.petrovis.connection.HttpConnector;
import mn.slide.petrovis.model.Aimag;
import mn.slide.petrovis.model.Sum;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class BranchesActivity extends Activity
{
	ExpandableListView	exList;
	DisplayMetrics		metrics;
	List<Aimag> aimags;
	List<List<Sum>>  lists;
	@Override
	public void onCreate( Bundle savedInstanceState )
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.branchs_activity);
		new DownloadXmlTask().execute();
	


	}

	private class DownloadXmlTask extends AsyncTask<Void, Void, List<Aimag>>
	{

		@Override
		protected List<Aimag> doInBackground( Void... params )
		{
			return ((HttpConnector) ((PetrovisApplication) getApplication()).getHttpConnector()).getAimag(BranchesActivity.this, HttpConnector.BRANCH_UB);
		}

		@Override
		protected void onPostExecute( List<Aimag> result )
		{
			if (result != null && result.size() != 0)
			{
				aimags = new ArrayList<Aimag>();
				aimags =result;
				lists = new ArrayList<List<Sum>>();
				for(Aimag aimag : aimags){
					lists.add(aimag.sums);
				}
				MyExpandableListAdapter adapter = new MyExpandableListAdapter(BranchesActivity.this, lists, aimags);
				ExpandableListView listView = (ExpandableListView)findViewById(R.id.list);
				listView.setAdapter(adapter);
				listView.setGroupIndicator(null);
				listView.setDivider(null);
			}
			else
			{
				// connection error
			}
			super.onPostExecute(result);
		}
	}

	private class MyExpandableListAdapter extends BaseExpandableListAdapter
	{
		private List<List<Sum>>	childList;
		private List<Aimag>		groupList;

		public MyExpandableListAdapter( Context context , List<List<Sum>> childList , List<Aimag> groupList )
		{
			this.childList = childList;
			this.groupList = groupList;
		}

		public Object getChild( int groupPosition , int childPosition )
		{
			return null;
		}

		public int getChildrenCount( int groupPosition )
		{
			List<Sum> temp = childList.get(groupPosition);
			return temp.size();
		}

		public LinearLayout getGenericView()
		{
			metrics = new DisplayMetrics();
			getWindowManager().getDefaultDisplay().getMetrics(metrics);
			int width = metrics.widthPixels;
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width - 50, LinearLayout.LayoutParams.WRAP_CONTENT);
			LinearLayout layout = new LinearLayout(BranchesActivity.this);
			layout.setOrientation(LinearLayout.HORIZONTAL);
			layout.setGravity(Gravity.CENTER_VERTICAL);
			TextView textView = new TextView(BranchesActivity.this);
			textView.setSingleLine();
			textView.setLayoutParams(lp);
			textView.setGravity(Gravity.CENTER_VERTICAL);
			textView.setPadding(15, 0, 0, 0);
			textView.setTextColor(Color.BLACK);
			RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			ImageView indicator = new ImageView(BranchesActivity.this);
			indicator.setPadding(0, 10, 0, 0);
			indicator.setImageResource(R.drawable.ic_indicator_empty);
			indicator.setLayoutParams(layoutParams);
			layout.addView(textView);
			layout.addView(indicator);
			return layout;
		}

		public View getChildView( final int groupPosition , int childPosition , boolean isLastChild , View convertView , ViewGroup parent )
		{
			LinearLayout layout = getGenericView();
			TextView textView = (TextView) layout.getChildAt(0);
			textView.setPadding(20, 0, 0, 0);
			textView.setText(childList.get(groupPosition).get(childPosition).name+" - "+childList.get(groupPosition).get(childPosition).description);
			textView.setGravity(Gravity.CENTER_VERTICAL);
			layout.setOnClickListener(new View.OnClickListener()
			{
				public void onClick( View v )
				{
					
				}
			});

			return layout;
		}

		public Object getGroup( int groupPosition )
		{
			return null;
		}

		public View getGroupView( int groupPosition , boolean isExpanded , View convertView , ViewGroup parent )
		{
			LinearLayout layout = getGenericView();
			if(groupPosition%2==0){
				layout.setBackgroundResource(R.drawable.nik1);
			}
			else{
				layout.setBackgroundResource(R.drawable.nik2);
			}
			TextView textView = (TextView) layout.getChildAt(0);
			textView.setGravity(Gravity.CENTER_VERTICAL);
			textView.setText(groupList.get(groupPosition).desc);
			if (isExpanded)
			{
				ImageView indicator = (ImageView) layout.getChildAt(1);
				indicator.setImageResource(R.drawable.ic_indicator_maximized);
			}
			else
			{
				ImageView indicator = (ImageView) layout.getChildAt(1);
				indicator.setImageResource(R.drawable.ic_indicator_minimized);
			}

			return layout;
		}

		public boolean isChildSelectable( int groupPosition , int childPosition )
		{
			return true;
		}

		public boolean hasStableIds()
		{
			return true;
		}

		@Override
		public long getChildId( int groupPosition , int childPosition )
		{
			return 0;
		}

		@Override
		public int getGroupCount()
		{
			return groupList.size();
		}

		@Override
		public long getGroupId( int groupPosition )
		{
			// TODO Auto-generated method stub
			return 0;
		}
	}
}